package com.cartoon.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.alibaba.android.arouter.utils.TextUtils;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.SPStaticUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.cartoon.R;
import com.cartoon.adapter.KKSearchCarttonAdapter;
import com.cartoon.adapter.LRYSearchAnimeAdapter;
import com.cartoon.databinding.CarttonSearchRecordActivityBinding;
import com.cartoon.model.CartoonDataViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;

import anime.room.HistoryModel;
import anime.room.HistoryViewModel;
import base.QActivity;
import base.QLoading;
import cartoon.room.CartoonHistoryModel;
import cartoon.room.CartoonHistoryViewModel;
import constant.AppConstants;
import http.Api;
import model.CarrtonKKSearchModel;
import model.CartoonLRYSearchModel;
import model.CarttonKKContentModel;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CarttonSearchRecordActivity extends QActivity {

    private String searchName;
    private com.cartoon.databinding.CarttonSearchRecordActivityBinding viewDataBinding;
    private CarttonSearchRecordActivityBinding binding;
    private QLoading dialog;
    private CartoonHistoryViewModel viewModel;
    private KKSearchCarttonAdapter kkSearchCarttonAdapter;
    private LRYSearchAnimeAdapter lrySearchAnimeAdapter;


    @Override
    public int getLayoutRes() {
        return R.layout.cartton_search_record_activity;
    }

    @Override
    public void initView() {
        Bundle bundle = getIntent().getExtras();
        searchName = bundle.getString("searchName");
        binding = DataBindingUtil.setContentView(this, getLayoutRes());
        viewModel = new ViewModelProvider(this).get(CartoonHistoryViewModel.class);

    }

    @Override
    public void initData() {
        binding.edSearch.setText(searchName);


        LinearLayoutManager kklayoutManager = new LinearLayoutManager(this);
        kklayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvKksearch.setLayoutManager(kklayoutManager);
        kkSearchCarttonAdapter = new KKSearchCarttonAdapter(R.layout.kk_search_cartton_item, null);
        binding.rvKksearch.setAdapter(kkSearchCarttonAdapter);



        LinearLayoutManager lrylayoutManager = new LinearLayoutManager(this);
        lrylayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rvLrysearch.setLayoutManager(lrylayoutManager);
        lrySearchAnimeAdapter = new LRYSearchAnimeAdapter(R.layout.lry_search_cartton_item, null);
        binding.rvLrysearch.setAdapter(lrySearchAnimeAdapter);


    }

    @Override
    public void initListener() {



        binding.edSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                //以下方法防止两次发送请求
                if (actionId == EditorInfo.IME_ACTION_SEND ||
                        (event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                    switch (event.getAction()) {
                        case KeyEvent.ACTION_UP:
                            getLRYSearchContext();
                            return true;
                        default:
                            return true;
                    }
                }
                return false;
            }
        });
        binding.edSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                searchName = binding.edSearch.getText().toString().trim();
                getTypeSearch(searchName);

            }
        });

        binding.ivSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getLRYSearchContext();
            }
        });


        kkSearchCarttonAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CarrtonKKSearchModel.HitsBean item = (CarrtonKKSearchModel.HitsBean) adapter.getData().get(position);
                int kkId = item.getId();
                Intent intent = new Intent(CarttonSearchRecordActivity.this, CartoonDetailsActivity.class);
                intent.putExtra("type", 1);
                intent.putExtra("kkid", kkId);
                startActivity(intent);
            }
        });


        lrySearchAnimeAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CartoonLRYSearchModel.DataBean item = (CartoonLRYSearchModel.DataBean)adapter.getData().get(position);
                String lryId = item.getComicId();
                Intent intent = new Intent(CarttonSearchRecordActivity.this, CartoonDetailsActivity.class);
                intent.putExtra("lryId", lryId);
                intent.putExtra("type", 2);
                startActivity(intent);
            }

        });
    }

    @Override
    public void networkMessage() {
        dialog = new QLoading();
        dialog.showNow(getSupportFragmentManager(), "");



        getTypeSearch(searchName);
    }


    public void getLRYSearchContext() {
        dialog = new QLoading();
        dialog.showNow(getSupportFragmentManager(), "");
        searchName = binding.edSearch.getText().toString().trim();

        if (TextUtils.isEmpty(searchName)) {
            ToastUtils.showLong("请输入搜索内容");
            return;
        }

        int searchId = SPStaticUtils.getInt("CartoonsearchId", 0);
        int newsearchId = searchId + 1;
        LogUtils.d("保存的Id" + newsearchId);
        SPStaticUtils.put("CartoonsearchId", newsearchId);
        viewModel.addHistoryDao(new CartoonHistoryModel(newsearchId, searchName));
        viewModel.getAllSLive().getValue();

        getTypeSearch(searchName);

    }


    public void getTypeSearch(String searchName) {

        int anInt = SPStaticUtils.getInt(AppConstants.SWITCHKEY, 1);
        if (anInt == 1) {
            getKKSearch(searchName);
            binding.rvKksearch.setVisibility(View.VISIBLE);
            binding.rvLrysearch.setVisibility(View.GONE);
        } else if (anInt == 2) {
            getLRYSearch(searchName);
            binding.rvKksearch.setVisibility(View.GONE);
            binding.rvLrysearch.setVisibility(View.VISIBLE);
        }

    }


    private void getKKSearch(String searchName) {
        Call<CarrtonKKSearchModel> call = Api.getInstance().service.getKKCartoonSearch(searchName);
        call.enqueue(new Callback<CarrtonKKSearchModel>() {
            @Override
            public void onResponse(Call<CarrtonKKSearchModel> call, Response<CarrtonKKSearchModel> response) {
                LogUtils.d("网络请求数据" + call.toString(), response.body());

                if (response.body() != null && response.body().getHits() != null) {
                    kkSearchCarttonAdapter.replaceData(response.body().getHits());
                    if (dialog != null) {
                        dialog.dismiss();
                    }

                } else {
                    ToastUtils.showLong("没有找到该漫画");
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }


            }

            @Override
            public void onFailure(Call<CarrtonKKSearchModel> call, Throwable t) {
                LogUtils.d("请切换模式试下  网络请求失败:" + t.getMessage());
                ToastUtils.showLong("请切换模式  网络请求失败:" + t.getMessage());
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }

    private void getLRYSearch(String searchName) {
        Call<CartoonLRYSearchModel> call = Api.getInstance().service.getLRYCartoonSearch(searchName,1,30);
        call.enqueue(new Callback<CartoonLRYSearchModel>() {
            @Override
            public void onResponse(Call<CartoonLRYSearchModel> call, Response<CartoonLRYSearchModel> response) {
                LogUtils.d("网络请求数据" + call.toString(), response.body());

                if (response.body() != null && response.body().getData() != null) {
                    lrySearchAnimeAdapter.replaceData(response.body().getData());
                    if (dialog != null) {
                        dialog.dismiss();
                    }

                } else {
                    ToastUtils.showLong("没有找到该漫画");
                    if (dialog != null) {
                        dialog.dismiss();
                    }
                }


            }

            @Override
            public void onFailure(Call<CartoonLRYSearchModel> call, Throwable t) {
                LogUtils.d("请切换模式试下  网络请求失败:" + t.getMessage());
                ToastUtils.showLong("请切换模式  网络请求失败:" + t.getMessage());
                if (dialog != null) {
                    dialog.dismiss();
                }
            }
        });
    }


}
